package br.com.revenda.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.com.revenda.conexao.conectionFactory;
import br.com.revenda.modelo.Cargo;


public class CargoDAO {

	private Connection conn;
	
	public CargoDAO(){
		this.conn = new conectionFactory().getConnection();
	}
	
	
	public void salvar(Cargo c){
		String sql = "insert into cargo (nomeCargo , salario)values(? , ?)";
		try {
			PreparedStatement pstm = conn.prepareStatement(sql);
			pstm.setString(1, c.getNome());
			pstm.setDouble(2, c.getSalario());
			pstm.execute();
			pstm.close();
			conn.close();
		} catch (SQLException e) {
			throw new RuntimeException(e);
	}		
}
	
	public void deletar(Cargo c){
		String sql = "delete from cargo where idCargo=?";
		
		try {
			PreparedStatement pstm = conn.prepareStatement(sql);
			pstm.setInt(1, c.getIdCargo());
			pstm.execute();
			pstm.close();
			conn.close();			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void alterar(Cargo c){
		String sql = "update cargo set nomeCargo=?, salario=? where idCargo=?";
		try {
			PreparedStatement pstm = conn.prepareStatement(sql);
			pstm.setString(1, c.getNome());
			pstm.setDouble(2, c.getSalario());
			pstm.setInt(3, c.getIdCargo());
			pstm.execute();
			pstm.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public List<Cargo> ListaTodos(){
		List<Cargo> f = new ArrayList<Cargo>();
		String sql = "select * from cargo";
		try {
			
			PreparedStatement pstm = conn.prepareStatement(sql);
			ResultSet rs = pstm.executeQuery();
			
			while(rs.next()){
				Cargo fn = new Cargo();
				fn.setIdCargo(rs.getInt("idcargo"));
				fn.setNome(rs.getString("nomeCargo"));
				fn.setSalario(rs.getDouble("salario"));
				f.add(fn);	
				
			}
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return f;
	}
	
	
	
	
	
	
	
}
